﻿using OrderStateMachineWebApi.Models;
using OrderStateMachineWebApi.Services;

namespace OrderStateMachineWebApi.ServicesImpl
{

    public class PaidState : IOrderState
    {
        public OrderStatus Status => OrderStatus.Paid;

        public Task PayAsync(Order order)
        {
            throw new InvalidOperationException("已支付订单不能重复支付");
        }

        public async Task ProcessAsync(Order order)
        {
            // 执行处理逻辑
            await Task.Delay(200); // 模拟处理时间

            // 状态转换
            order.Status = OrderStatus.Processing;
            order.UpdatedAt = DateTime.UtcNow;
            order.Notes += $"[{DateTime.UtcNow:o}] 订单开始处理; ";
        }

        public Task ShipAsync(Order order)
        {
            throw new InvalidOperationException("已支付订单需先处理才能发货");
        }

        public Task CompleteAsync(Order order)
        {
            throw new InvalidOperationException("已支付订单需先处理才能完成");
        }

        public Task CancelAsync(Order order)
        {
            throw new InvalidOperationException("已支付订单不能直接取消，请申请退款");
        }

        public async Task StartRefundAsync(Order order)
        {
            // 执行开始退款逻辑
            await Task.Delay(150); // 模拟退款处理时间

            // 状态转换
            order.Status = OrderStatus.Refunding;
            order.UpdatedAt = DateTime.UtcNow;
            order.Notes += $"[{DateTime.UtcNow:o}] 订单开始退款; ";
        }

        public Task CompleteRefundAsync(Order order)
        {
            throw new InvalidOperationException("需先发起退款");
        }
    }

}
